//
//  BaseCircularButton.swift
//  OnceNectar
//
//  Created by 法正磊 on 2018/2/1.
//  Copyright © 2018年 com.inwhoop. All rights reserved.
//

import UIKit

class BaseCircularButton: UIButton {

    var buttonSize: CGSize = .zero
    var borderColor: UIColor
    init(title: String, fontSize:Int, titleSideOffset: CGFloat, titleColor:UIColor, borderColor: UIColor) {
        self.borderColor = borderColor
        super.init(frame: .zero)
        let size = MyConfigTool.myConfigWithLabelTextRect(with: CGSize(width: ScreenWidth, height: CGFloat(MAXFLOAT)), withFontSize: fontSize, withText: title).size
        self.setTitle(title, for: .normal)
        self.titleLabel?.font = UIFont.systemFont(ofSize: CGFloat(fontSize))
        self.setTitleColor(titleColor, for: .normal)
        buttonSize = CGSize(width: size.width + titleSideOffset * 2, height: size.height + 4)
    }
    init(title: String, fontSize:Int, titleSideOffset: CGFloat, titleColor:UIColor,titleSelectColor:UIColor, borderColor: UIColor) {
        self.borderColor = borderColor
        super.init(frame: .zero)
        let size = MyConfigTool.myConfigWithLabelTextRect(with: CGSize(width: ScreenWidth, height: CGFloat(MAXFLOAT)), withFontSize: fontSize, withText: title).size
        self.setTitle(title, for: .normal)
        self.titleLabel?.font = UIFont.systemFont(ofSize: CGFloat(fontSize))
        self.setTitleColor(titleColor, for: .normal)
        self.setTitleColor(titleColor, for: .selected)
        buttonSize = CGSize(width: size.width + titleSideOffset * 2, height: size.height + 4)
    }
    override func draw(_ rect: CGRect) {
        let borderWidth: CGFloat = 2
        let maskPath = UIBezierPath(roundedRect: self.bounds, cornerRadius: rect.size.height)
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
        let path = UIBezierPath(roundedRect: self.bounds, cornerRadius: rect.size.width + borderWidth)
        let lineLayer = CAShapeLayer()
        lineLayer.path = path.cgPath
        lineLayer.fillColor = UIColor.clear.cgColor
        lineLayer.lineWidth = borderWidth
        lineLayer.strokeColor = borderColor.cgColor
        self.layer.addSublayer(lineLayer)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
